<?php
	mysql_connect("localhost", "root", "");
	mysql_select_db("ps3_theatre");
	session_start();

	//upgrade from version 0.1 to 0.2
	if($_SESSION['version'] == 'V0.1 01/03/2010'){
		//create tbl_cacheimage 
		mysql_query("CREATE TABLE IF NOT EXISTS `tbl_cacheimage` (
						`id` int(6) NOT NULL AUTO_INCREMENT,
						`type_id` int(6) NOT NULL,
						`link_id` int(6) NOT NULL,
						`keyword` varchar(512) NOT NULL,
						`subfolder_path` varchar(1024) NOT NULL,
						`filename` varchar(512) NOT NULL,
						PRIMARY KEY (`type_id`,`keyword`,`id`),
						KEY `id` (`id`)
					) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());
		
		//create table tbl_errorstrings
		mysql_query("CREATE TABLE IF NOT EXISTS `tbl_errorstrings` (
	  					`id` int(6) NOT NULL AUTO_INCREMENT,
  						`error_string` varchar(512) NOT NULL,
  						PRIMARY KEY (`id`)
					) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;") or die(mysql_error());
					
		//fill table tbl_errorstrings
		mysql_query("INSERT INTO `tbl_errorstrings` (`id`, `error_string`) VALUES
						(1, 'More than 1 movie result returned, adding year to filename might help return fewer results.  Click rescan and select the correct movie.'),
						(2, 'No Results returned. Title may be mispelled.'),
						(3, 'Invalid file name.  Name needs to have series title and (S##E##)'),
						(4, 'Series couldn''t be found'),
						(5, 'Episode information couldn''t be found'),
						(6, 'More than 1 series result returned.');") or die(mysql_error());
					
		//insert new rows in tbl_fileextensions
		mysql_query("INSERT INTO `tbl_fileextensions` (`type_id`, `extension`, `is_active`) VALUES
					(1, 'm4v', 'Y'),
					(2, 'm4v', 'Y');") or die(mysql_error());
					
		//add error_id to tbl_movies
		mysql_query("ALTER TABLE `tbl_movies` ADD `error_id` INT( 6 ) NULL AFTER `tmdb_id`;") or die(mysql_error());
		
		//remove bg_dir from tbl_settings
		mysql_query("ALTER TABLE `tbl_settings` DROP `bg_dir`;") or die(mysql_error());
		
		//change version #
		mysql_query("UPDATE `tbl_settings` set version='V0.2 03/10/2011';") or die(mysql_error());
		
		//create table tbl_tvshow_episode
		mysql_query("CREATE TABLE IF NOT EXISTS `tbl_tvshow_episode` (
	  					`id` int(6) NOT NULL AUTO_INCREMENT,
  						`directory_id` int(6) NOT NULL,
  						`subfolder_path` varchar(1024) NOT NULL,
  						`filename` varchar(512) NOT NULL,
  						`tvdbepisode_id` int(10) NOT NULL,
  						`series_id` int(10) NOT NULL,
  						`error_id` int(6) DEFAULT NULL,
  						`season` int(6) NOT NULL,
  						`episode` int(6) NOT NULL,
  						`airdate` varchar(10) NOT NULL,
  						`name` varchar(512) NOT NULL,
  						`description` varchar(1024) NOT NULL,
  					PRIMARY KEY (`id`)
					) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());
					
		//create table tbl_tvshow_series
		mysql_query("CREATE TABLE IF NOT EXISTS `tbl_tvshow_series` (
	  					`id` int(6) NOT NULL AUTO_INCREMENT,
  						`tvdbseries_id` int(10) NOT NULL,
  						`name` varchar(512) NOT NULL,
  						`description` varchar(1024) NOT NULL,
  						`content_rating` varchar(10) NOT NULL,
  						`first_aired` varchar(10) NOT NULL,
  						`genre` varchar(256) NOT NULL,
  						`network` varchar(30) NOT NULL,
  						`network_image` varchar(128) NOT NULL,
  						`rating` double NOT NULL,
  						`runtime` int(4) NOT NULL,
  					PRIMARY KEY (`id`)
					) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());
					
		//add displayed column to tbl_types
		mysql_query("ALTER TABLE `tbl_types` ADD `displayed` char(1) NOT NULL AFTER `order`;") or die(mysql_error());
		
		//update displayed to 'Y' for all cols
		mysql_query("UPDATE `tbl_types` set displayed='Y';") or die(mysql_error());
				
		//insert row into tbl_types
		mysql_query("INSERT INTO `tbl_types` (`type`, `theme_id`, `order`, `displayed`) VALUES
					('Main', 0, 0,'N');") or die(mysql_error());
		
		//refresh page to check for more updates		
		header( 'Location: upgradeDatabase.php');
		
	//upgrade from version 0.2 to 0.3
	}else if($_SESSION['version'] == 'V0.2 03/10/2011'){
	
		//add watched column to tbl_movies
		mysql_query("ALTER TABLE `tbl_movies` ADD `is_watched` char(1) NOT NULL AFTER `audio_flag_id`;") or die(mysql_error());
  
		//add 2 new theme entries for weather and random
		mysql_query("INSERT INTO `tbl_themes` (`id`, `name`, `type_id`, `url`) VALUES
					(4, 'Weather', 5, '');") or die(mysql_error());
					
		mysql_query("INSERT INTO `tbl_themes` (`id`, `name`, `type_id`, `url`) VALUES
					(5, 'Random', 6, '');") or die(mysql_error());
  
		//add watched column to tbl_tvshow_episode
		mysql_query("ALTER TABLE `tbl_tvshow_episode` ADD `is_watched` char(1) NOT NULL AFTER `description`;") or die(mysql_error());

		//add 2 new types entries for weather and random
		mysql_query("INSERT INTO `tbl_types` (`id`, `type`, `theme_id`, `order`, `displayed`) VALUES
					(5, 'Weather', 4, 4, 'Y');") or die(mysql_error());
					
		mysql_query("INSERT INTO `tbl_types` (`id`, `type`, `theme_id`, `order`, `displayed`) VALUES
					(6, 'Random', 5, 5, 'Y');") or die(mysql_error());
					
		mysql_query("UPDATE tbl_types SET displayed='N' WHERE id=3;") or die(mysql_error());

		//create table tbl_wakeonlan					
		mysql_query("CREATE TABLE IF NOT EXISTS `tbl_wakeonlan` (
					`id` int(6) NOT NULL AUTO_INCREMENT,
  					`mac_address` varchar(17) NOT NULL,
  					`secure_on` varchar(100) NOT NULL,
  					`broadcast_address` varchar(50) NOT NULL,
  					`cidr` int(2) NOT NULL,
  					`port` int(5) NOT NULL,
  					PRIMARY KEY (`id`)
				) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;") or die(mysql_error());
			
		//update version #
		mysql_query("UPDATE tbl_settings SET version='V0.3 06/29/2011';") or die(mysql_error());

		//refresh page to check for more updates		
		header( 'Location: upgradeDatabase.php');
	}
	
	if(isset($_SESSION['version'])){			
		unset($_SESSION['version']);
		unset($_SESSION['moveThe']);
		unset($_SESSION['cacheHttpDir']);
		unset($_SESSION['cacheCompDir']);
	}

	header( 'Location: index.php?message=Database sucessfully upgraded' ) ;
?>